home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8081 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.ld.centuryinter.net!usenet
  2. From: steidl@centuryineter.net
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Run time dynamic 2-D array?
  5. Date: 14 Feb 1996 11:48:38 GMT
  6. Organization: Century Internet    
  7. Message-ID: <4fsi6m$maf@news.ld.centuryinter.net>
  8. References: <4flcq9$i1k@vixen.cso.uiuc.edu>
  9. Reply-To: steidl@centuryineter.net
  10. NNTP-Posting-Host: anx_p12.wi.centuryinter.net
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4flcq9$i1k@vixen.cso.uiuc.edu>, WEIMIN YANG <w-yang3> writes:
  14. >I need to use a 2-D array. I use following code.
  15. >
  16. >void tryit(int a, int b) {
  17. >
  18. >float c= new float[b][a];
  19. >
  20. >....
  21. >
  22. >.....
  23. >
  24. >}
  25. >
  26. >I get compile error. Is there another way to do it?
  27.  
  28. Neither C nor C++ maintain any run-time information about arrays.  When you
  29. access an element of a 2-D array such as x[a,b] code is generated to calculate
  30. the address of the particular element using a formula something like:
  31.    x + (a * max_columns + b)
  32. [I can't remember if C is column-major or row-major, and it's really not relevent
  33. here.]
  34. Since C does not maintain any run-time info on arrays (presumably because
  35. this would lead to a slower implementation of arrays), and since it does need
  36. max_columns to calculate element addresses, the inevitable result is that
  37. max_columns is a compile-time constant and that an array can only be of
  38. variable size in one its dimensions.
  39.  
  40. >By the way, I don't want to use 1-D array to implement it.
  41.  
  42. Well, you have three choices:
  43.     1. Use linked-lists to implement it :), or
  44.     2. Find an 2-D array class that someone else has already implemented, or
  45.     3. Pick a different programming language.
  46.  
  47.